package com.agileai.pm.module.proc.exteral;

import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.pm.cxmodule.ProcWeeklyManage;

public class ProjWeeklyImpl extends BaseRestService implements ProjWeekly{

	@Override
	public String findProjWeeklyInfos(String projectId,String num) {
		String result = "";
		try {
			ProcWeeklyManage getService = this.lookupService(ProcWeeklyManage.class);
			DataParam param = new DataParam();
			param.put("projectId", projectId);
			List<DataRow> weeklyList = getService.findRecords(param);
			int displayNum=1; //每页显示多少条
			
			int startNum=(Integer.parseInt(num)-1)*displayNum;//当前页数，从第几条开始显示
			List<DataRow> weeklyListPage = getService.findWeeklyListRecords(projectId,startNum,displayNum);
			int weeklyListSize = weeklyList.size();//总数据条数
			int weeklyListPageSize = weeklyListPage.size();
			JSONArray jsonArray = new JSONArray();
			
			for(int i = 0; i < weeklyListPage.size(); i++){
				JSONObject jsonObject = new JSONObject();
				DataRow weeklyInfo = weeklyListPage.get(i);
				String weeklyId = weeklyInfo.stringValue("PWI_ID");
				DataParam detailedTypeParam = new DataParam();
				detailedTypeParam.put("PWI_ID", weeklyId);
				//名称和邮件主题放入json
				String projectName = weeklyInfo.stringValue("PWI_NAME");
				String mailTheme = weeklyInfo.stringValue("PWI_EMAILL_THEME");
				projectName=projectName.split("&nbsp;")[1];
				jsonObject.put("projectName", projectName);
				jsonObject.put("mailTheme", mailTheme);
				jsonObject.put("weeklyListSize", weeklyListSize);
				jsonObject.put("weeklyListPageSize", weeklyListPageSize);
				
				detailedTypeParam.put("PWD_TYPE", "THIS_WEEKLY");
				List<DataRow> thisWeeklyList = getService.findDetailedRecords(detailedTypeParam);
				
				JSONArray thisWeeklyJsonArray = new JSONArray();
				for(int twl = 0; twl < thisWeeklyList.size(); twl++){
					JSONObject thisWeeklyJsonObject = new JSONObject();
					DataRow rowThisWeekly = thisWeeklyList.get(twl);
					String content = rowThisWeekly.stringValue("PWD_CONTENT");
					String charge = rowThisWeekly.stringValue("PWD_CHARGE");
					String workload = rowThisWeekly.stringValue("PWD_WORKLOAD");
					String satrtTime = rowThisWeekly.stringValue("PWD_SATRT_TIME");
					String state = rowThisWeekly.stringValue("PWD_STATE_NAME");
					String explain = rowThisWeekly.stringValue("PWD_EXPLAIN");
					thisWeeklyJsonObject.put("content", content);
					thisWeeklyJsonObject.put("charge", charge);
					thisWeeklyJsonObject.put("workload", workload);
					thisWeeklyJsonObject.put("satrtTime", satrtTime);
					thisWeeklyJsonObject.put("state", state);
					thisWeeklyJsonObject.put("explain", explain);
					thisWeeklyJsonArray.put(thisWeeklyJsonObject);
				}
				jsonObject.put("thisWeekly", thisWeeklyJsonArray);
				
				detailedTypeParam.put("PWD_TYPE", "NEXT_WEEKLY");
				List<DataRow> nextWeeklyList = getService.findDetailedRecords(detailedTypeParam);
				
				JSONArray nextWeeklyListJsonArray = new JSONArray();
				
				for(int nwl = 0; nwl < nextWeeklyList.size(); nwl++){
					JSONObject nextWeeklyListJsonObject = new JSONObject();
					DataRow rowNextWeekly = nextWeeklyList.get(nwl);
					String content = rowNextWeekly.stringValue("PWD_CONTENT");
					String charge = rowNextWeekly.stringValue("PWD_CHARGE");
					String workload = rowNextWeekly.stringValue("PWD_WORKLOAD");
					String satrtTime = rowNextWeekly.stringValue("PWD_SATRT_TIME");
					String state = rowNextWeekly.stringValue("PWD_STATE_NAME");
					String explain = rowNextWeekly.stringValue("PWD_EXPLAIN");
					nextWeeklyListJsonObject.put("content", content);
					nextWeeklyListJsonObject.put("charge", charge);
					nextWeeklyListJsonObject.put("workload", workload);
					nextWeeklyListJsonObject.put("satrtTime", satrtTime);
					nextWeeklyListJsonObject.put("state", state);
					nextWeeklyListJsonObject.put("explain", explain);
					nextWeeklyListJsonArray.put(nextWeeklyListJsonObject);
				}
				jsonObject.put("nextWeekly", nextWeeklyListJsonArray);
				
				detailedTypeParam.put("PWD_TYPE", "FOLLOW_WEEKLY");
				List<DataRow> followWeeklyList = getService.findDetailedRecords(detailedTypeParam);
				
				JSONArray followWeeklyJsonArray = new JSONArray();
				
				for(int nwl = 0; nwl < followWeeklyList.size(); nwl++){
					JSONObject followWeeklyJsonObject = new JSONObject();
					DataRow rowFollowWeekly = followWeeklyList.get(nwl);
					String content = rowFollowWeekly.stringValue("PWD_CONTENT");
					String charge = rowFollowWeekly.stringValue("PWD_CHARGE");
					String workload = rowFollowWeekly.stringValue("PWD_WORKLOAD");
					String satrtTime = rowFollowWeekly.stringValue("PWD_SATRT_TIME");
					String state = rowFollowWeekly.stringValue("PWD_STATE_NAME");
					String explain = rowFollowWeekly.stringValue("PWD_EXPLAIN");
					followWeeklyJsonObject.put("content", content);
					followWeeklyJsonObject.put("charge", charge);
					followWeeklyJsonObject.put("workload", workload);
					followWeeklyJsonObject.put("satrtTime", satrtTime);
					followWeeklyJsonObject.put("state", state);
					followWeeklyJsonObject.put("explain", explain);
					followWeeklyJsonArray.put(followWeeklyJsonObject);
				}
				jsonObject.put("followWeekly", followWeeklyJsonArray);
				jsonArray.put(jsonObject);
			}
			result = jsonArray.toString();
		} catch (JSONException e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
	} 
}